package cool.tianyou.blue.bird.dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.google.common.collect.ImmutableMap;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import cool.tianyou.blue.bird.R;
import cool.tianyou.blue.bird.util.HttpUtil;

public class RequestUnlockDialog extends AlertDialog {
    private final Context context;
    private final String alias;
    private final Integer deviceId;
    private Integer autoDismiss;

    public RequestUnlockDialog(@NonNull Context context, String alias, Integer deviceId) {
        super(context);
        this.context = context;
        this.alias = alias;
        this.deviceId = deviceId;
    }

    public RequestUnlockDialog setAutoDismiss(Integer autoDismiss) {
        this.autoDismiss = autoDismiss;
        return this;
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_loading_layout);
        initView();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initView() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // 加载布局文件
        @SuppressLint("InflateParams") View mLayout = inflater.inflate(R.layout.dialog_request_unlock_layout, null, false);

        // 添加布局文件到 Dialog
        this.setContentView(mLayout);
        TextView tvAlias = mLayout.findViewById(R.id.tv_device_alias);
        tvAlias.setText(alias);
        mLayout.findViewById(R.id.btn_unlock).setOnClickListener(v -> {
            ImmutableMap<Object, Object> param = ImmutableMap.builder().put("lock", false)
                    .put("deviceId", deviceId)
                    .build();
            HttpUtil.put(context, param, "/device/status", apiResult -> {
                this.dismiss();
            });


        });
    }

    public RequestUnlockDialog showDialog() {
        this.show();
        return this;
    }

    @Override
    public void show() {
        this.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        Window dialogWindow = this.getWindow();

        dialogWindow.setWindowAnimations(R.style.DialogOutAndInStyle);
        super.show();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL);
        lp.width = 800; // 宽度
        dialogWindow.setAttributes(lp);
        this.setCanceledOnTouchOutside(false);

        if (this.autoDismiss != null) {
            ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
            executor.schedule(this::dismiss, autoDismiss, TimeUnit.MILLISECONDS);
        }

    }
}
